package com.mgface.eth.api;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.http.HttpService;

import java.math.BigInteger;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project eth
 * @desc 获取随机数
 * @create 2018-06-26 22:07
 **/
public class GetNonce {

    public static BigInteger getNonce(String address) throws Exception {
        Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/IY77yuopq4bSgHoaFqJe"));
        //获取nonce
        EthGetTransactionCount ethGetTransactionCount = web3.ethGetTransactionCount(address, DefaultBlockParameterName.LATEST).sendAsync().get();
        //
        BigInteger nonce = ethGetTransactionCount.getTransactionCount();
        System.out.println("当前交易的序列号:"+nonce);
        return nonce;
    }

    public static void main(String[] args) throws Exception {
        getNonce("0x70982B82D6EdFDC91B0998c62954e703B7407747");
    }
}
